package com.cn.algorithm.one.heap;

import java.util.Comparator;

public class PriorityQueue<E> {

    BinaryHeap<E> heap;

    public PriorityQueue(Comparator<E> comparator) {
        heap = new BinaryHeap<>(comparator);
    }

    public PriorityQueue() {
        this(null);
    }

    public int size() {
        return heap.size();
    }

    public boolean isEmpty() {
        return heap.isEmpty();
    }

    public void clear() {
        heap.clear();
    }

    public void enQueue(E element) {
        heap.add(element);
    }

    public E deQueue() {
        return heap.pop();
    }

    public E replace(E element) {
        return heap.replace(element);
    }

    public E front() {
        return heap.front();
    }


    public static void main(String[] args) {
        // 新建一个小顶堆
        PriorityQueue<Integer> heap = new PriorityQueue<>(new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });

        // 找出最大的前k个数
        int k = 3;
        Integer[] data = {51, 30, 39, 92, 74, 25, 16, 93,
                91, 19, 54, 47, 73, 62, 76, 63, 35, 18,
                90, 6, 65, 49, 3, 26, 61, 21, 48};
        for (int i = 0; i < data.length; i++) {
            if (heap.size() < k) { // 前k个数添加到小顶堆
                heap.enQueue(data[i]); // logk
            } else if (data[i] > heap.front()) { // 如果是第k + 1个数，并且大于堆顶元素
                heap.replace(data[i]); // logk
            }
        }
        System.out.println(heap.size());
    }

}
